package com.comdo.newup_loan.module.repayment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.adapter.RePayListAdapter;
import com.comdo.newup_loan.base.BaseFragment;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.model.RepayListBean;
import com.comdo.newup_loan.module.home.SuggestionActivity;
import com.comdo.newup_loan.module.home.WantLoanActivity;
import com.comdo.newup_loan.widget.MyItemDecoration;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class RePaymentFragment extends BaseFragment<RePayListPersenter> implements RepayListContact.View,View.OnClickListener{
    private View view;
    @BindView(R.id.recyclerview)
    XRecyclerView mRecyclerView;
    @BindView(R.id.ll_to_loan)
    LinearLayout mToLoanLayout;
    @BindView(R.id.img_suggest)
    ImageView mSuggestImg;
    private RePayListAdapter mAdapter;
    private ArrayList<RepayListBean> listData;
    private int page = 1;
    private Intent intent;
    //页面是否可见
    private boolean isHidden;
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_repayment;
    }

    @Override
    protected void init() {

        isHidden=false;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        mToLoanLayout.setOnClickListener(this);
        mSuggestImg.setOnClickListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new MyItemDecoration());
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);

        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mPresenter.getList(Config.loginInfo.getUserId(), page);
            }

            @Override
            public void onLoadMore() {
                page++;
                mPresenter.getList(Config.loginInfo.getUserId(), page);
            }
        });

        //mRecyclerView.setEmptyView();
        listData = new ArrayList<RepayListBean>();
        mAdapter = new RePayListAdapter(listData,getActivity());
        mAdapter.setOnItemClickListener(new RePayListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getActivity(),RePaymentDetailActivity.class);
                intent.putExtra("tenderId",listData.get(position).getId());
                getActivity().startActivity(intent);

            }
        });
        mRecyclerView.setAdapter(mAdapter);

    }

    @Override
    public RePayListPersenter getmPresenter() {
        return new RePayListPersenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }

    @Override
    public void getFail(String failMessage) {
        showToast(failMessage);
        mRecyclerView.refreshComplete();
    }

    @Override
    public void getList(List<RepayListBean> datas) {
        if(page==1)
            listData.clear();
        listData.addAll(datas);
        mAdapter.notifyDataSetChanged();
        mRecyclerView.refreshComplete();
       // if(datas.size()< Config.pageSize)
           // mRecyclerView.setNoMore(true);
        if(listData.size()==0){
            mToLoanLayout.setVisibility(View.VISIBLE);
        }else{
            mToLoanLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_to_loan:
                Intent intent=new Intent(getActivity(), WantLoanActivity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.img_suggest:
                intent = new Intent(getActivity(), SuggestionActivity.class);
                getActivity().startActivity(intent);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if(!isHidden){
            mRecyclerView.refresh();
        }
    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        isHidden = hidden;
    }
}
